<?php
/*专业，模型*/
class Major_model extends CI_Model
{
    public $table = 'major';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 返回数据表总条目个数
     * @return [type] [description]
     */
    public function count_all()
    {
        return $this->db->count_all($this->table);
    }

    /**
     * 得到某个学校的专业的数量
     * @return [type] [description]
     */
    public function count_by_school_id($school_id)
    {
        $sql = 'SELECT count(*) as count FROM major WHERE school_id=?';
        $res = $this->db->query($sql, $school_id)->row_array();
        return $res['count'];
    }
    /**
     * 获得所有的条目
     * @return [type] [description]
     */
    public function get_all()
    {
        $res = $this->db->get($this->table)->result_array();
        return $res;
    }

    /**
     * 通过id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_by_id($id)
    {
        return $this->db->get_where($this->table, array('id' => $id))->row_array();
    }
    /**
     * 通过school_id返回条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_by_school_id($school_id)
    {
        return $this->db->get_where($this->table, array('school_id' => $school_id))->result_array();
    }
    /**
     * 通过school_id返回一个条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function get_one_major_by_school_id($school_id)
    {
        return $this->db->get_where($this->table, array('school_id' => $school_id))->row_array();
    }
    /**
     * 添加一个条目
     * @param [type] $item [description]
     */
    public function add($item)
    {
        return $this->db->insert($this->table, $item);
    }

    /**
     * 删除一个条目
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function delete($id)
    {
        $sql = 'SELECT * FROM teacher as t WHERE t.major_id=?';
        $teacher_row = $this->db->query($sql, $id)->row_array();
        if ($teacher_row) {
            return false;
        }
        $sql = 'SELECT * FROM student as s WHERE s.major_id=?';
        $student_row = $this->db->query($sql, $id)->row_array();
        if ($student_row) {
            return false;
        }
        return $this->db->delete($this->table, array('id' => $id));
    }

    /**
     * 更新一个条目
     * @param  [type] $id   [description]
     * @param  [type] $item [description]
     * @return [type]       [description]
     */
    public function update($id, $item)
    {
        $this->db->where('id', $id);
        return $this->db->update($this->table, $item);
    }


    /**
     * 检查专业名称是否已经存在
     * @return [type] [description]
     */
    public function check_major_name_exist_in_a_school($major_name, $school_id)
    {
        $sql = 'SELECT count(*) as count FROM major as m WHERE m.name=? and m.school_id=?';
        $row = $this->db->query($sql, array($major_name, $school_id))->row_array();
        return ($row['count'] > 0);
        // return $row;
    }

    /**
     * 通过专业名称，得到专业
     * @param  [type] $major_name [description]
     * @return [type]             [description]
     */
    public function get_by_major_name($major_name)
    {
        $sql = 'SELECT * FROM major WHERE name=?';
        return $this->db->query($sql, $major_name)->row_array();
    }
}
